const request = require('request')
const fs = require('fs')
const path = require('path')
const URL = require("url");

function getRes(url) {
    return new Promise((resolve, reject) => {
        request.get(url).on('response', response => {
            const fileName = path.basename(URL.parse(url).pathname);
            console.log(fileName)
            const fileWriteStream = fs.createWriteStream(path.resolve(__dirname, fileName))
            response.pipe(fileWriteStream)
            resolve()
        }).on('error', reject)
    });
}

const urls = [
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190428/201904281412457316223.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20180127/201801271402412722186.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190226/201902261054483725746.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/201510/2015101411245757.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190313/201903130951457372109.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190402/201904021219498146575.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190528/201905281430297410849.jpg',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190521/201905211559442021496.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190520/201905201525389086361.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190307/201903071412306446937.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190130/201901301128292645531.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190102/201901021319523128012.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20190225/201902251053143607981.png',
    'https://imgwsdl.vivo.com.cn/appstore/developer/icon/20181025/201810251914039366723.png'
]

function downloadAll() {
    Promise.all(urls.map(getRes)).then(() => {
        console.log('complete')
    })
}

const arr = [
    '2015101411245757',
    '201801271402412722186',
    '201810251914039366723',
    '201901021319523128012',
    '201901301128292645531',
    '201902251053143607981',
    '201902261054483725746',
    '201903071412306446937',
    '201903130951457372109',
    '201904021219498146575',
    '201904281412457316223',
    '201905201525389086361',
    '201905211559442021496',
    '201905281430297410849',
]

function generateMainfest() {
    const group = {
        name: 'icons',
        keys: arr.join(',')
    }

    const resouces = arr.map((name) => ({
        name,
        type: 'image',
        url: `icons/${name}.png`
    }))

    console.log(JSON.stringify({
        group,
        resouces
    }, '', 2))
}

generateMainfest()
